/*
 * _______________#########_______________________ 
 * ______________############_____________________ 
 * ______________#############____________________ 
 * _____________##__###########___________________ 
 * ____________###__######_#####__________________ 
 * ____________###_#######___####_________________ 
 * ___________###__##########_####________________ 
 * __________####__###########_####_______________ 
 * ________#####___###########__#####_____________ 
 * _______######___###_########___#####___________ 
 * _______#####___###___########___######_________ 
 * ______######___###__###########___######_______ 
 * _____######___####_##############__######______ 
 * ____#######__#####################_#######_____ 
 * ____#######__##############################____ 
 * ___#######__######_#################_#######___ 
 * ___#######__######_######_#########___######___ 
 * ___#######____##__######___######_____######___ 
 * ___#######________######____#####_____#####____ 
 * ____######________#####_____#####_____####_____ 
 * _____#####________####______#####_____###______ 
 * ______#####______;###________###______#________ 
 * ________##_______####________####______________ 
 * 
 * @Author: ipk518 18163976442@163.com
 * @Date: 2024-06-19 10:31:14
 * @LastEditors: ipk518 18163976442@163.com
 * @LastEditTime: 2024-08-01 16:00:15
 * @FilePath: /etws-sxk2307/src/Service/Server.cpp
 * @Description: 
 * 
 * Copyright (c) 2024 by etws@quyujiang, All Rights Reserved. 
 */

#include "commHeader.h"
#include "Server.h"
#include "Common.h"


#include "Ad9171.h"
#include "Lmx2820.h"
#include "Ina3221.h"
#include "nodeConf.h"
#include "lxCmdV1.h"
#include "Mem.h"
#include "Uio.h"
#include "Gpio.h"
#include "autoCalibration.h"
#include "autoTbf.h"
#include "Tu.h"
#include "gnss.h"
//随机数
#include <ctime>
#include <random>

/*主题使用: 0 controlPub和controlSub
*		   1 rwDDRPub和rwDDRSub
*		   2 queryPub和querySub
*		   3 errorReportPub和errorReportSub
*	       4 logPub和logSub
*		   5 serverCmdPub和serverCmdSub
*		   6 heartBeatPub和heartBeatSub
*		   7 wavePub和waveSub
***********************************/

const EventType ParameterInit[] = {
WRITE_BEAM_PARAM,
WRITE_BEAM_LIST,
WRITE_DBF_REG,
WRITE_MANUALCALI_COE,
WRITE_AUTOCALI_COE,
// WRITE_DBF_POINT,
// WRITE_HARDWARE_TEST,
WRITE_PHASECODE

};

static int hrSendto(struct cmdDataType* cmd,uint8_t * cmdData);

Server::Server()
{
	// TODO Auto-generated constructor stub
	confStructInit();
}

Server::~Server()
{
	isRunning = false;
	if (m_obsSub != nullptr) {
		m_sxk.m_uio->m_pub->remove(m_obsSub);
		delete m_obsSub;
		m_obsSub = nullptr;
	}

	if (m_calibSub!= nullptr) {
		m_sxk.m_uio->m_pub->remove(m_calibSub);
		delete m_calibSub;
		m_calibSub = nullptr;
	}

	if (m_autoCalibSub!= nullptr) {
		m_sxk.m_uio->m_pub->remove(m_autoCalibSub);
		delete m_autoCalibSub;
		m_autoCalibSub = nullptr;
	}

	if (m_autoTbfSub!= nullptr) {
		m_sxk.m_uio->m_pub->remove(m_autoTbfSub);
		delete m_autoTbfSub;
		m_autoTbfSub = nullptr;
	}
	if (m_ctrlDds != nullptr) {
		delete m_ctrlDds;
        m_ctrlDds = nullptr;
	}

	if (m_queryDds != nullptr) {
		delete m_queryDds;
        m_queryDds = nullptr;
	}

	if (m_hrDds != nullptr) {
		delete m_hrDds;
        m_hrDds = nullptr;
	}
	
	if (m_hrServer != nullptr) {
		delete m_hrServer;
		m_hrServer = nullptr;
	}
	if (m_serverDds != nullptr) {
		delete m_serverDds;
		m_serverDds = nullptr;
	}

	if (ddsPort != nullptr) {
		delete ddsPort;
        ddsPort = nullptr;
	}
	if (lxPort != nullptr) {
		delete lxPort;
        lxPort = nullptr;
	}
	if (queryPort != nullptr) {
		delete queryPort;
        queryPort = nullptr;
	}
	if (sdsPort != nullptr) {
		delete sdsPort;
		sdsPort = nullptr;
	}

	if (sdsFd != nullptr) {
		sdsFd->join();
		delete sdsFd;
	}
	if (ddsFd != nullptr) {
		ddsFd->join();
		delete ddsFd;
	}
	if (lxFd != nullptr) {
		lxFd->join();
		delete lxFd;
	}
	if (queryFd != nullptr) {
		queryFd->join();
		delete queryFd;
	}
	if(waveFd != nullptr) {
		waveFd->join();
		delete waveFd;
	}
	if(handleFd != nullptr) {
		handleFd->join();
		delete handleFd;
	}
	if (m_led != nullptr) {
		delete m_led;
	}
	if (m_obsSub != nullptr) {
		delete m_obsSub;
		delete m_calibSub;
	}
	m_sxk.deInit();
}


//暗室PC数据解析
static int tcpDataCallback(struct cmdDataType* cmd,uint8_t * cmdData)
{
	int len = cmd->dataLen;
	len = len -1;
	static struct cmdDataType tcpCmd;
	static struct dataType tcpLx;
	if(sysRunStatus == SYS_RUN)
	{
		qlog_i("Enter TCPDataCallback ");
		memcpy(&tcpLx,cmdData,sizeof(struct dataType));
		if(tcpLx.head == PROTOCOL_HEAD && tcpLx.tail == PROTOCOL_TAIL){
			memset(&tcpCmd,0,sizeof(struct cmdDataType));
			tcpCmd.source_DevType = DEBUG_V20_Sub_Unit_S;
			tcpCmd.cmdType = tcpLx.cmdType;
			tcpCmd.cmdChildType = tcpLx.cmdChildType;
			tcpCmd.dst_DevType = WAVE_TIMING_Sub_Unit_S;
			tcpCmd.dst_DevNum = 0;
			tcpCmd.dataLen = tcpLx.dataLen;
			/*****************************************解析雷象上位机的指令**************************************************/
			qlog_hexdump("darkPc cmd",16,(uint8_t *)&tcpCmd,sizeof(struct cmdDataType));
			lxProtocolAnalysis(&tcpCmd,(cmdData+sizeof(struct dataType)));
			/************************************************************************************************/
		}
	}
	return 0;
}

/**
 * @brief: 主动心跳上报,带时间戳的心跳包发出(暂时不要)
 * @param {cmdDataType*} cmd
 * @param {uint8_t *} cmdData
 * @return {*}
 */
static int hrSendto(struct cmdDataType* cmd,uint8_t * cmdData)
{
	if(sysRunStatus == SYS_RUN)
	{
		// uint8_t gpsTime[12];
		// cmd->dataLen = 12;

		// gpsTime[0] = (uint8_t)sxk_tools.m_sxkInfo.m_gpsStatus.m_year;
		// gpsTime[1] = (uint8_t)(sxk_tools.m_sxkInfo.m_gpsStatus.m_year >> 8);
		// gpsTime[2] = (uint8_t)sxk_tools.m_sxkInfo.m_gpsStatus.m_month;
		// gpsTime[3] = (uint8_t)(sxk_tools.m_sxkInfo.m_gpsStatus.m_month >> 8);
		// gpsTime[4] = (uint8_t)sxk_tools.m_sxkInfo.m_gpsStatus.m_day;
		// gpsTime[5] = (uint8_t)(sxk_tools.m_sxkInfo.m_gpsStatus.m_day >> 8);


		// gpsTime[6] = (uint8_t)sxk_tools.m_sxkInfo.m_gpsStatus.m_hour;
		// gpsTime[7] = (uint8_t)(sxk_tools.m_sxkInfo.m_gpsStatus.m_hour >> 8);
		// gpsTime[8] = (uint8_t)sxk_tools.m_sxkInfo.m_gpsStatus.m_min;
		// gpsTime[9] = (uint8_t)(sxk_tools.m_sxkInfo.m_gpsStatus.m_min >> 8);
		// gpsTime[10] = (uint8_t)sxk_tools.m_sxkInfo.m_gpsStatus.m_sec;
		// gpsTime[11] = (uint8_t)(sxk_tools.m_sxkInfo.m_gpsStatus.m_sec >> 8);

		// //时钟+8时区处理
		// gpsTime[6] = gpsTime[6] - 0x30;
		// gpsTime[7] = gpsTime[7] - 0x30 + 8;
		// if(gpsTime[7] > 9) {
		// 	gpsTime[6] = gpsTime[7]/10 + 0x30;
		// 	gpsTime[7] = gpsTime[7]%10 + 0x30;
		// }
		Server::Instance().separate(s_sds,0xffff,cmd,cmdData);
	}
	return 0;
}

Server& Server::Instance()
{
	static Server m_instance;
	return m_instance;
}



int Server::init()
{
	printf("%s",look);
	//注册日志输出
	logProtocol::Instance().init(t_logReport,"logPub");
	//初始化日志输出级别
	_SETLOG_LEVEL_(Conf::currentConf()->mLogConf.uLogOut);

	qlog_i("ServerInit!");
	pthread_setname_np(pthread_self(),"x05_Server");
	auto_version();
	//led control
	if(Conf::currentConf()->mTestConf.uInitPl) {
		m_led = new Led();
		m_led->setStatus(led_red);
	}
	//创建解析器
	ddsPort = new ddsResolver(string("ddsPort"));
	ddsPort->init(200);

	sdsPort = new ddsResolver(string("sdsPort"));
	sdsPort->init(200);

	queryPort = new ddsResolver(string("queryPort"));
	queryPort->init(100);

	lxPort = new lxResolver();
	lxPort->init(100*1024);

	wavePort = new ddsResolver(string("wavePort"));
	wavePort->init(100);
	
	auto m_localCmdIp = Conf::currentConf()->mSessConf.uLocalCmdIp.c_str();
	auto m_localCmdPort = Conf::currentConf()->mSessConf.uCmdPort;
	auto m_localHtbIp = Conf::currentConf()->mSessConf.uLocalHtbIp.c_str();
	auto m_localHtbPort = Conf::currentConf()->mSessConf.uHtbPort;
	auto m_pcIp = Conf::currentConf()->mSessConf.uPcIp.c_str();
	auto m_pcPort = Conf::currentConf()->mSessConf.uPcPort;
	qlog_n("localCmdIp:%s,port:%d",m_localCmdIp,m_localCmdPort);
	qlog_n("localHrIp:%s,port:%d",m_localHtbIp,m_localHtbPort);
	qlog_n("darktsPcIp:%s,darktsPcPort:%d",m_pcIp,m_pcPort);


	//初始化controldds主题，注册解析器...
	m_ctrlDds = new ddsProtocol(ddsPort);
	m_ctrlDds->init(t_controlCmd,"controlPub","controlSub");
	//初始读写DDR内存主题，注册解析器...
	m_ddrDds = new ddsProtocol(ddsPort);
	m_ddrDds->init(t_rwDdr,"rwDDRSub","rwDDRPub");
	//初始化参数查询主题，注册解析器...
	m_queryDds = new ddsProtocol(queryPort);
	m_queryDds->init(t_query,"queryPub","querySub");
	//初始化心跳传输主题
	m_hrDds = new ddsProtocol(queryPort);
	m_hrDds->init(t_heartBeat,"heartBeatPub","heartBeatSub");
	//初始化serverCmddds主题，注册解析器...
	m_serverDds = new ddsProtocol(sdsPort);
	m_serverDds->init(t_serverReplyCmd,"serverCmdSub","serverCmdPub");
	//初始化波形读取主题
	m_iqDds = new ddsProtocol(wavePort);
	m_iqDds->init(t_iqCmd,"waveSub","wavePub");
	
	//初始化心跳服务器
	m_hrServer  = new hrServer();
	m_hrServer->Init(hrSendto);

	//初始化雷象命令端口，注册解析器...
	lxSession::Instance().Register(lxPort);
	lxSession::Instance().Init(m_localCmdIp,m_localCmdPort);
	//初始化雷象心跳端口...
	hrSession::Instance().Init(m_localHtbIp,m_localHtbPort);
	//初始化暗室PC端口,注册回调函数...
	tcpSock::Instance().Init(m_pcIp,m_pcPort);
	tcpSock::Instance().Register(tcpDataCallback);

	//TODO:2025-9-27
	gnssModbusRtu::Instance().Init("192.168.23.14",62307);

	//硬件初始化
	if(Conf::currentConf()->mTestConf.uInitPl) {
		auto ret = m_sxk.Init();
		if(false == ret) { //硬件未通过，关闭正常运行
			return -1;
		}
		
		if(m_sxk.m_uio != nullptr) {
			//初始化客户端子节点
			m_obsSub = new iqObserve();
			m_calibSub = new iqCalibration();
			m_autoCalibSub = new crontab_uio();
			m_autoTbfSub = new autoTbf();
			//添加观测通道子节点
			m_sxk.m_uio->m_pub->attach(m_obsSub);
			m_sxk.m_uio->m_pub->attach(m_calibSub);
			m_sxk.m_uio->m_pub->attach(m_autoCalibSub);
			m_sxk.m_uio->m_pub->attach(m_autoTbfSub);
		}

		m_led->setStatus(led_green);
		//初始化事件触发器
		m_event.addEvent(WRITE_CONTROL_REG,std::bind(&Server::actTimingGenerator,this));
		m_event.addEvent(WRITE_HARDWARE_TEST,std::bind(&Server::actHardwareTest,this));
		m_event.addEvent(WRITE_BEAM_PARAM,std::bind(&Server::actBeamReg,this));
		m_event.addEvent(WRITE_BEAM_LIST,std::bind(&Server::actListReg,this));
		m_event.addEvent(WRITE_DBF_REG,std::bind(&Server::actDbfReg,this));
		m_event.addEvent(WRITE_MANUALCALI_COE,std::bind(&Server::actManualCaliReg,this));
		m_event.addEvent(WRITE_AUTOCALI_COE,std::bind(&Server::actAutoCalilReg,this));
		m_event.addEvent(WRITE_DBF_POINT,std::bind(&Server::actDbfPointReg,this));
		m_event.addEvent(WRITE_PHASECODE,std::bind(&Server::actPhaseCodeReg,this));
		
		//参数初始化流程
		for(int i = 0; i < (int)(sizeof(ParameterInit)/sizeof(EventType));i++)
		{
			m_event.triggerEvent(ParameterInit[i]);
		}
		if ( Conf::currentConf()->mTestConf.uStartTime > 0 ) {
			m_sxk.m_controlReg.m_timingStatus = TIME_START;
			m_event.triggerEvent(WRITE_CONTROL_REG);
		}

	}
	
	isRunning = true;
	lxFd = new std::thread(std::bind(&Server::lxProtocolTask,this));
	sdsFd = new std::thread(std::bind(&Server::sdsProtocolTask,this));
	waveFd = new std::thread(std::bind(&Server::waveProtocolTask,this));
	ddsFd = new std::thread(std::bind(&Server::ddsProtocolTask,this));
	queryFd = new std::thread(std::bind(&Server::queryProtocolTask,this));
	handleFd = new std::thread(std::bind(&Server::processTask,this));
	return 0;
}


bool Server::run()
{
	static int process_5s = 0;
	bool *m_adOnce = nullptr;
	m_event.start();
	sysRunStatus = SYS_RUN;
	m_hrFlag = (Conf::currentConf()->mTestConf.uHrEnable > 0);
	m_plInit = (Conf::currentConf()->mTestConf.uInitPl > 0);
	if (m_plInit) {
		m_sxk.dataInit();//TODO:2025-8-27
		m_adOnce = &m_sxk.m_once;
	}
	while(isRunning) 
	{
		if(m_hrFlag) {
			//心跳运行
			m_hrServer->Run();
			if(*m_adOnce && m_hrServer->isAlive(AD_Sub_Unit_S,0) && m_hrServer->isAlive(AD_Sub_Unit_S,1)) { // AD在线
				*m_adOnce = false;
				m_sxk.initADSync();//TODO:2025/3/27
			}
			//同时掉线的情况下，开启自动同步
			if( (*m_adOnce == false) && (!m_hrServer->isAlive(AD_Sub_Unit_S,0)) && (!m_hrServer->isAlive(AD_Sub_Unit_S,1))) {
				*m_adOnce = true;
			}
		}
		if (m_plInit) {
			//硬件运行
			m_sxk.run();
		}
		// qlog_d("Server is running...");
		if (process_5s >= PROCESS_5S) {
			process_5s = 0;
			if( !m_sxk.checkClockLockStatus() ) {
				if(m_led->getStatus() != led_red) {
					m_led->setStatus(led_red);
				}
			} else {
				if(m_led->getStatus() == led_red) {
					m_led->setStatus(led_green);
				}
			}
		} else {
			process_5s++;
		}
		usleep(HEARTBERT_TIME_US); // 500ms
	}
	m_event.stop();
	return false;
}



void Server::actTimingGenerator()
{
	qlog_n("ControlReg triggered !");
	m_sxk.setSysTimingPara();
}


void Server::actHardwareTest()
{
	qlog_n("HardwareTest triggered !");
	m_sxk.setHardwareTestPara();
}


void Server::actListReg()
{
	qlog_n("BeamList triggered !");
	m_sxk.setBeamListPara();
}

void Server::actBeamReg()
{
	qlog_n("BeamParam triggered !");
	m_sxk.setBeamPara();
}

void Server::actAutoCalilReg()
{
	qlog_n("AutoCalilReg triggered!");
	m_sxk.setAutoCaliPara();
}

void Server::actManualCaliReg()
{
	qlog_n("CouplingCoe triggered !");
	m_sxk.setManualCaliPara();
}

void Server::actDbfReg()
{
	qlog_n("DBFCoe triggered !");
	m_sxk.setDBFPara();
}

void Server::actDbfPointReg()
{
	qlog_n("DBFPoint triggered!");
	m_sxk.setDBFPointPara();
}

void Server::actPhaseCodeReg()
{
	qlog_n("PhaseCode triggered!");
	m_sxk.setPhaseCodePara();
}

bool Server::createSharePtr(Response* m)
{
	if (m_webResponse == nullptr) {
		m_webResponse = m;
	} else {
		return false;
	}
	return true;
}

Response* Server::getSharePtr()
{
	if (m_webResponse != nullptr) {
		return m_webResponse;
	} else {
		return nullptr;
	}
}

void Server::delSharePtr()
{
	m_webResponse = nullptr;
}


size_t Server::separate(route r,int num,uint8_t * cmdData,size_t len)
{
	size_t ret = 0;
	lock_guard<mutex> guard(m_sendMtx);
	switch(r)
	{
		case s_mtuart:
			qlog_i("servoMotor send data.");
			qlog_hexdump("Motor data:",16,cmdData,len);
			// setGpioValue(m_sxk.m_rs485De,1);
			ret = m_sxk.m_mtUart->sendto(cmdData,len);
			// sleep(1);
			// setGpioValue(m_sxk.m_rs485De,0);
			break;
		case s_pc:
			ret = tcpSock::Instance().sendto(cmdData,len);//发往暗室PC
		break;
		default:
				break;
	}
	return ret;
}

int Server::separate(route r,int num,struct cmdDataType* cmd,uint8_t * cmdData)
{
	int ret = 0;
	{
		lock_guard<mutex> guard(m_sendMtx);
		switch (r)
		{
			case s_all:
					m_ctrlDds->publish(cmd,cmdData);
					ret = lxSession::Instance().sendto(cmd,cmdData);
					return ret;
					break;
			case s_web:
					ret = lxSession::Instance().sendto(cmd,cmdData);//时序卡自身指令
					return ret;
					break;
			case s_pc:
					ret = tcpSock::Instance().sendto(cmd,cmdData);//发往暗室PC
					return ret;
					break;
			case s_hbt:
					ret = hrSession::Instance().sendto(cmd,cmdData);
					return ret;
					break;
			case s_sds:
					ret = m_serverDds->publish(cmd,cmdData);
					return ret;
					break;
			case s_udpcom:
					break;
			case s_rs422:
					break;
			case s_ddr:
					ret = m_ddrDds->publish(cmd,cmdData);
					return ret;
					break;
			// case s_hrDds:
			// 		m_hrDds->publish(cmd,cmdData);
			//     	break;
			case s_iq:
					ret = m_iqDds->publish(cmd,cmdData);
					return ret;
					break;
			default:
				break;
		}
	}
    
	int i=0;
    do{
		switch(r) 
		{
			case s_dds:
				if (num == 0xFFFF) {
					cmd->DeviceType = t_controlCmd;
					cmd->dst_DevNum = allDevice;
					ret = m_ctrlDds->publish(cmd,cmdData);
					break;
				} else {
					if (((num>>i)&0x01) > 0) {
						cmd->DeviceType = t_controlCmd;
						cmd->dst_DevNum = i;
						ret = m_ctrlDds->publish(cmd,cmdData);
					}
				}
			break;
			case s_queryDds:
				if (num == 0xFFFF) {
					cmd->DeviceType = t_query;
					cmd->dst_DevNum = allDevice;
					ret = m_queryDds->publish(cmd,cmdData);
					break;
				} else {
					if (((num>>i)&0x01) > 0) {
						cmd->DeviceType = t_query;
						cmd->dst_DevNum = i;
						ret = m_queryDds->publish(cmd,cmdData);
					}
				}
			break;
			case s_hrDds:
				if (num == 0xFFFF) {
					cmd->DeviceType = t_controlCmd;
					cmd->dst_DevNum = allDevice;
					ret = m_hrDds->publish(cmd,cmdData);
					break;
				} else {
					if (((num>>i)&0x01) > 0) {
						cmd->DeviceType = t_controlCmd;
						cmd->dst_DevNum = i;
						ret = m_hrDds->publish(cmd,cmdData);
					}
				}
			break;
			default:i=16;break;
		}
	i++;
	}while(i < 16);

	return ret;
}

/*** 
 * @description: 雷象上位机响应
 * @param {cmdDataType*} replyCmd
 * @return {*}
 */
int Server::responseWeb(struct cmdDataType* replyCmd)
{
	struct cmdDataType cmdHeader;
    uint16_t sendnum = 0;  
	cmdHeader.DeviceType = t_controlCmd;
	cmdHeader.source_DevType = CurDeviceType;
	cmdHeader.source_DevNum = ComponentNO;
	cmdHeader.dst_DevType =  leixing_SERVER_Sub_Unit_S;
	cmdHeader.dst_DevNum = 0;
	cmdHeader.cmdType = replyCmd->cmdType;
	cmdHeader.cmdChildType = replyCmd->cmdChildType;
	cmdHeader.dataLen = 0;
	return separate(s_web,sendnum,&cmdHeader,nullptr);
}

int Server::responseSds(struct cmdDataType* replyCmd)
{
	struct cmdDataType cmdHeader;
    uint16_t sendnum = 0;  
	cmdHeader.DeviceType = t_controlCmd;
	cmdHeader.source_DevType = CurDeviceType;
	cmdHeader.source_DevNum = ComponentNO;
	cmdHeader.dst_DevType =  CENTER_SERVER_Sub_Unit_S;
	cmdHeader.dst_DevNum = 0;
	cmdHeader.cmdType = replyCmd->cmdType;
	cmdHeader.cmdChildType = replyCmd->cmdChildType;
	cmdHeader.dataLen = 0;
	return separate(s_sds,sendnum,&cmdHeader,nullptr);
}

/*** 
 * @description: 观测通道应答路线，由中断程序触发此回调
 * @param {int} m_flag
 * @return {*}
 */
void iqObserve::update(int m_flag,vector<uint8_t>& dataIn)
{
    uioMode m = static_cast<uioMode>(m_flag);
    struct cmdDataType cmdHeader;
    cmdHeader.DeviceType = t_iqCmd;
    cmdHeader.source_DevType = CurDeviceType;
    cmdHeader.source_DevNum = ComponentNO;
    cmdHeader.dst_DevType =  CENTER_SERVER_Sub_Unit_S;
    cmdHeader.dst_DevNum = 0;
	cmdHeader.DevStatus = CMD_OK;
	switch (m)
	{
		case uio_tx_observer:
			/* code */
			cmdHeader.cmdType = d_sxCmd;
			cmdHeader.cmdChildType = 0;
			break;
		case uio_rx_observer:
			cmdHeader.cmdType = d_sxCmd;
			cmdHeader.cmdChildType = 1;
			break;
		case uio_adc_observer:
			cmdHeader.cmdType = d_sxCmd;
			cmdHeader.cmdChildType = 2;
			break;
		case uio_dbf_observer:
			cmdHeader.cmdType = d_sxCmd;
			cmdHeader.cmdChildType = 3;
			break;
		
		default:
			cmdHeader.cmdType = d_sxCmd;
			cmdHeader.cmdChildType = 255;
			cmdHeader.DevStatus = CMD_INVAL;
			break;
	}
	if (m == uio_tx_observer || m == uio_rx_observer
		|| m == uio_adc_observer || m == uio_dbf_observer) {

		cmdHeader.dataLen = dataIn.size();
    	Server::Instance().separate(s_iq,0,&cmdHeader,dataIn.data());
	}
    
    
}

/*** 
 * @description: 校准数据应答路线，由中断程序触发此回调
 * @param {int} m_flag
 * @return {*}
 */
void iqCalibration::update(int m_flag,vector<uint8_t>& dataIn)
{
    uioMode m = static_cast<uioMode>(m_flag);
    struct cmdDataType cmdHeader;
	memset(&cmdHeader,0,sizeof(cmdHeader));
    cmdHeader.DeviceType = t_serverReplyCmd;
    cmdHeader.source_DevType = CurDeviceType;
    cmdHeader.source_DevNum = ComponentNO;
	cmdHeader.dataLen = dataIn.size();
	if (m_ackChannel == 0) {
		// cmdHeader.dst_DevType =  leixing_SERVER_Sub_Unit_S;
		// cmdHeader.dst_DevNum = 0;
		// switch(m)
		// {
		// 	case uio_h_sendPolar:
		// 		cmdHeader.cmdType = (uint8_t)(hSendCali >> 16);
		// 		cmdHeader.cmdChildType = (uint8_t)(hSendCali);
		// 	break;
		// 	case uio_v_sendPolar:
		// 		cmdHeader.cmdType = (uint8_t)(vSendCali >> 16);
		// 		cmdHeader.cmdChildType = (uint8_t)(vSendCali);
		// 	break;
		// 	case uio_h_recvicePolar:
		// 		cmdHeader.cmdType = (uint8_t)(hRecvCali >> 16);
		// 		cmdHeader.cmdChildType = (uint8_t)(hRecvCali);
		// 	break;
		// 	case uio_v_recvicePolar:
		// 		cmdHeader.cmdType = (uint8_t)(vRecvCali >> 16);
		// 		cmdHeader.cmdChildType = (uint8_t)(vRecvCali);
		// 	break;
		// 	default:
		// 	break;
		// }
		// if(m == uio_h_recvicePolar || m == uio_v_recvicePolar) {//TODO:2025-2-13 2025-07-5发射和接收都由自动校准应答
		// 	Server::Instance().separate(s_web,0,&cmdHeader,dataIn.data());	
		// }
		
	} else if(m_ackChannel == 1) { //小上位机
		cmdHeader.dst_DevType =  CENTER_SERVER_Sub_Unit_S;
		cmdHeader.dst_DevNum = 0;
		switch(m)
		{
			case uio_h_sendPolar:
				cmdHeader.cmdType = 2;
				cmdHeader.cmdChildType = 5;
			break;
			case uio_v_sendPolar:
				cmdHeader.cmdType = 2;
				cmdHeader.cmdChildType = 6;
			break;
			case uio_h_recvicePolar:
				cmdHeader.cmdType = 2;
				cmdHeader.cmdChildType = 7;
			break;
			case uio_v_recvicePolar:
				cmdHeader.cmdType = 2;
				cmdHeader.cmdChildType = 8;
			break;
			default:
			break;
		}
		if (m != uio_autoMode) {
			Server::Instance().separate(s_sds,0,&cmdHeader,dataIn.data());
		}
		
	}
    
	
	m_ackChannel = 0;
}

void iqCalibration::test(char* m,int len)
{
	qlog_i("iqCalibration test %c",*m);
}

void iqObserve::test(char* m,int len)
{
	qlog_i("iqObserve test %c",*m);
}
 
/*** 
 * @description: 模拟校准信号
 * @return {*}
 */
bool Server::caliAnalog(timeStatus st)
{
    if(Conf::currentConf()->mTestConf.uCalibAnalog != 0) {
		static uint16_t dataOut[64];
		for(int i = 0; i<32;i++)
		{
			dataOut[i] = getIntRandom(Conf::currentConf()->mTestConf.uAmpAnalog1,Conf::currentConf()->mTestConf.uAmpAnalog2);
			dataOut[i+32] = getIntRandom(Conf::currentConf()->mTestConf.uPhaseAnglog1,Conf::currentConf()->mTestConf.uPhaseAnglog2);
		}
        struct cmdDataType cmdHeader;
		memset(&cmdHeader,0,sizeof(cmdHeader));
		cmdHeader.DeviceType = t_controlCmd;
		cmdHeader.source_DevType = CurDeviceType;
		cmdHeader.source_DevNum = ComponentNO;

		if (m_calibSub->m_ackChannel == 0) {
			cmdHeader.dst_DevType =  leixing_SERVER_Sub_Unit_S;
			cmdHeader.dst_DevNum = 0;

			cmdHeader.dataLen = 32*2*2;//32通道的幅相，每个占2字节
			switch(st)
			{
				case TIME_H_SEND_CALL:
					cmdHeader.cmdType = (uint8_t)(hSendCali >> 16);
					cmdHeader.cmdChildType = (uint8_t)(hSendCali);
				break;
				case TIME_V_SEND_CALL:
					cmdHeader.cmdType = (uint8_t)(vSendCali >> 16);
					cmdHeader.cmdChildType = (uint8_t)(vSendCali);
				break;
				case TIME_H_REVICE_CALL:
					cmdHeader.cmdType = (uint8_t)(hRecvCali >> 16);
					cmdHeader.cmdChildType = (uint8_t)(hRecvCali);
				break;
				case TIME_V_REVICE_CALL:
					cmdHeader.cmdType = (uint8_t)(vRecvCali >> 16);
					cmdHeader.cmdChildType = (uint8_t)(vRecvCali);
				break;
				default:
				break;
			}

			Server::Instance().separate(s_web,0,&cmdHeader,(uint8_t *)dataOut);
		}

		if (m_calibSub->m_ackChannel == 1) {
			cmdHeader.dst_DevType =  CENTER_SERVER_Sub_Unit_S;
			cmdHeader.dst_DevNum = 0;

			cmdHeader.dataLen = 32*2*2;//32通道的幅相，每个占2字节
			switch(st)
			{
				case TIME_H_SEND_CALL:
					cmdHeader.cmdType = 2;
					cmdHeader.cmdChildType = 5;
				break;
				case TIME_V_SEND_CALL:
					cmdHeader.cmdType = 2;
					cmdHeader.cmdChildType = 6;
				break;
				case TIME_H_REVICE_CALL:
					cmdHeader.cmdType = 2;
					cmdHeader.cmdChildType = 7;
				break;
				case TIME_V_REVICE_CALL:
					cmdHeader.cmdType = 2;
					cmdHeader.cmdChildType = 8;
				break;
				default:
				break;
			}

			Server::Instance().separate(s_sds,0,&cmdHeader,(uint8_t *)dataOut);
		}
		
    } else {
		return false;
	}
	m_calibSub->m_ackChannel = 0;
	return true;
	
	
}